package com.gaogzhen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.domain.AccountDetail;
import com.gaogzhen.dto.UserDto;
import com.gaogzhen.feigh.UserServiceFeign;
import com.gaogzhen.mapper.AccountDetailMapper;
import com.gaogzhen.service.AccountDetailService;
import com.gaogzhen.utils.Constants;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @description ${description}
* @author Administrator
* @date 2022-11-03 11:43
* @version 1.0
*/
@Service
public class AccountDetailServiceImpl extends ServiceImpl<AccountDetailMapper, AccountDetail> implements AccountDetailService{

    @Autowired
    private UserServiceFeign userServiceFeign;

    @Override
    public Page<AccountDetail> findByPage(Page<AccountDetail> page, Long coinId, Long userId, String username, String mobile, Long accountId, String numMin, String numMax, String startTime, String endTime) {
        // 按最后修改时间倒序查询
//        page.addOrder(OrderItem.desc("last_update_time"));
        LambdaQueryWrapper<AccountDetail> queryWrapper = new LambdaQueryWrapper<>();
        // 1 携带用户信息：用户名或者手机号
        Map<Long, UserDto> userDtoMap = null;
        Set<Long> ids = null;
        if (ObjectUtils.isNotEmpty(userId) || StringUtils.isNotEmpty(username) || StringUtils.isNotEmpty(mobile)) {
            // 远程调用
            userDtoMap = userServiceFeign.getBriefUsers(ObjectUtils.isEmpty(userId) ? null : Collections.singletonList(userId), username, mobile);
            if (MapUtils.isEmpty(userDtoMap)) {
                return new Page<>();
            }
            ids = userDtoMap.keySet();
            queryWrapper.in(CollectionUtils.isNotEmpty(ids), AccountDetail::getUserId, ids);
        }
        // 2 没有携带
        LambdaQueryWrapper<AccountDetail> wrapper = queryWrapper.eq(ObjectUtils.isNotEmpty(coinId), AccountDetail::getCoinId, coinId)
                .eq(ObjectUtils.isNotEmpty(accountId), AccountDetail::getAccountId, accountId)
                .between(
                        StringUtils.isNotEmpty(numMin) && StringUtils.isNotEmpty(numMax),
                        AccountDetail::getAmount,
                        numMin == null? null : new BigDecimal(numMin), numMax == null ? null: new BigDecimal(numMax))
                .between(
                        StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime),
                        AccountDetail::getCreated,
                        startTime, endTime + " 23:59:59");

        Page<AccountDetail> page1 = page(page, wrapper);
        List<AccountDetail> records = page1.getRecords();

        if (CollectionUtils.isNotEmpty(records)) {
            // 远程调用
            if (MapUtils.isEmpty(userDtoMap)) {
                List<Long> idList = records.stream().map(AccountDetail::getUserId).collect(Collectors.toList());
                userDtoMap = userServiceFeign.getBriefUsers(idList,null, null);
            }
            Map<Long, UserDto> finalUserDtoMap = userDtoMap;
            records.forEach(c -> {
                UserDto userDto = finalUserDtoMap.get(c.getUserId());
                if (userDto == null) {
                    c.setUsername(Constants.UNKNOWN_USER);
                    c.setRealName(Constants.UNKNOWN_USER);
                } else {
                    c.setUsername(userDto.getUsername());
                    c.setRealName(userDto.getRealName());
                }

            });
        }

        return page1;
    }
}
